﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Tp.Dto.Allgemein;
using Tp.Dto.Enums;
using Tp.Dto.EventArgs;
using Tp.Dto.Stammdaten;
using Tp.Interfaces.Commands.Stammdaten;
using Tp.Interfaces.Services;
using Tp.Interfaces.ViewModels.Stammdaten;


namespace Tp.StammdatenViewModel
{
    public class OrganisationUserControlViewModel : ViewModelBase, IOrganisationUserControlViewModel
    {

        #region Members

        private bool isInProcess = false;
        private List<OrganisationDto> organisationsList;
        private bool processIsStarting = false;
        private List<RechtsformDto> rechtsformen;
        private OrganisationDto selectedOrganisation;
        private IOrganisationService service;
        private string titel = "Organisation";
        private List<ZuordnungDto> zuordnungen;
        
        #endregion

        #region Properties

        public IOrganisationViewModelCommands Commands { get; set; }

        public bool IsInProcess
        {
            get { return this.isInProcess; }
            set
            {
                if (this.isInProcess == value)
                {
                    return;
                }
                this.isInProcess = value;
                RaisePropertyChanged(() => IsInProcess);
            }
        }

        public string MyTitel
        {
            get { return this.titel; }
            set { this.titel = value; }
        }

        public List<OrganisationDto> OrganisationList
        {
            get { return this.organisationsList; }
            set
            {
                if (this.organisationsList != null && this.organisationsList == value)
                {
                    return;
                }

                this.organisationsList = value;
                RaisePropertyChanged(() => OrganisationList);
            }
        }

        public bool ProcessIsStarting
        {
            get { return this.processIsStarting; }
            set
            {
                if (this.processIsStarting == value)
                {
                    return;
                }
                this.processIsStarting = value;
                RaisePropertyChanged(() => ProcessIsStarting);
            }
        }

        public List<RechtsformDto> Rechtsformen {
            get { return this.rechtsformen; }
            set {
                if (this.rechtsformen == value) {
                    return;
                }
                this.rechtsformen = value;
                RaisePropertyChanged(() => Rechtsformen);
            }
        }

        public OrganisationDto SelectedOrganisation
        {
            get { return this.selectedOrganisation; }
            set
            {
                if (this.selectedOrganisation == value)
                {
                    return;
                }

                if (this.selectedOrganisation != null)
                {
                    if (this.SelectedOrganisation.IsDirty)
                    {
                        MessageBoxResult res = MessageBox.Show("Daten wurden noch nicht gespeichert. Möchten Sie die Daten jetzt speichern?", "Organisation - " + this.selectedOrganisation.Name.ToString(), MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation);
                        switch (res)
                        {
                            case MessageBoxResult.Yes:
                                this.service.SaveOrganisation(selectedOrganisation);
                                break;
                            case MessageBoxResult.No:
                                this.SelectedOrganisation.UndoChanges();
                                break;
                            case MessageBoxResult.Cancel:
                                return;

                        }
                    }
                }

                this.selectedOrganisation = value;
                
                this.ProcessIsStarting = true;
                this.IsInProcess = false;
                if (this.SelectedOrganisation != null)
                {
                    this.SelectedOrganisation.IsDirty = false;
                }
                RaisePropertyChanged(() => SelectedOrganisation);
            }
        }

        public List<ZuordnungDto> Zuordnungen {
            get { return this.zuordnungen; }
            set {
                if (this.zuordnungen == value) {
                    return;
                }
                this.zuordnungen = value;
                RaisePropertyChanged(() => Zuordnungen);
            }
        }

        #endregion

        #region C-Tor

        public OrganisationUserControlViewModel(IOrganisationViewModelCommands commands, IOrganisationService service)
        {
            this.Commands = commands;
            this.service = service;
            this.RegisterMessengers();
            this.LoadRechtsformen();
            this.LoadZuordnungen();
        }

        #endregion

        #region Messengers

        private void RegisterMessengers()
        {
            Messenger.Default.Register<OrganisationOperationEnum.OperationsState>(this, ExecuteMessage);
            Messenger.Default.Register<IAbstractDto>(this, StartWorking);
            Messenger.Default.Register<OrganisationDto>(this, SaveModel);
            Messenger.Default.Register<string>(this, SelectAction);
        }

        #endregion

        #region Methods

        private void ExecuteMessage(OrganisationOperationEnum.OperationsState state)
        {

            switch (state)
            {
                case OrganisationOperationEnum.OperationsState.LoadOrganisations:
                    this.LoadOrganisations();
                    break;

            }

        }

        private void LoadOrganisations()
        {
            OrganisationList = this.service.LoadOrganisations();
            //OrganisationDto myOrg = new OrganisationDto(0, null, null, null, null, null, null, null, null, null, null, null);
            //myOrg.CanUndo = false;
            //SelectedOrganisation = myOrg;

        }

        private void LoadRechtsformen() {
            this.rechtsformen = new List<RechtsformDto>();
            this.rechtsformen.Add(new RechtsformDto(-1, "<Bitte auswählen>"));
            this.rechtsformen.Add(new RechtsformDto(6, "GmbH"));
            this.rechtsformen.Add(new RechtsformDto(7, "GmbH & Co KG"));
            this.rechtsformen.Add(new RechtsformDto(8, "KG"));
        }

        private void LoadZuordnungen() {
            this.zuordnungen = new List<ZuordnungDto>();
            this.zuordnungen.Add(new ZuordnungDto(-1, "<Bitte auswählen>"));
            this.zuordnungen.Add(new ZuordnungDto(1, "Kunde"));
            this.zuordnungen.Add(new ZuordnungDto(2, "Lieferant"));
            this.zuordnungen.Add(new ZuordnungDto(3, "Interessent"));
            this.zuordnungen.Add(new ZuordnungDto(4, "Einkauf"));
        }

        public bool OnCbObjectCheckBoxChecked(object sender, RoutedEventArgs e) {
            return false;
        }

        private void SaveModel(OrganisationDto organisation)
        {
            if (organisation != null)
            {
                if (MessageBox.Show("Wollen Sie wirklich speichern?", "Organisation - " + organisation.Name.ToString(), MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    this.service.SaveOrganisation(organisation);
                    this.IsInProcess = false;
                    this.ProcessIsStarting = true;
                    organisation.IsDirty = false;
                    organisation.CanUndo = false;
                }
            }
        }

        private void SelectAction(string action)
        {
            if (action != null)
            {
                switch (action)
                {
                    case "Undo":
                        this.SelectedOrganisation.UndoChanges();
                        this.SelectedOrganisation.Adresse.UndoChanges();
                        this.SelectedOrganisation.Telefon.UndoChanges();
                        this.SelectedOrganisation.Fax.UndoChanges();
                        this.SelectedOrganisation.IsDirty = false;
                        this.IsInProcess = false;
                        this.ProcessIsStarting = true;
                        this.SelectedOrganisation.CanUndo = false;
                        break;
                    default:
                        throw new NotImplementedException(string.Format("Message of type '{0}' is not implemented", action));
                }
            }
        }

        private void StartWorking(IAbstractDto abstractDto)
        {
            if (abstractDto != null)
            {
                IsInProcess = true;
                ProcessIsStarting = false;
                //this.SelectedOrganisation = abstractDto as OrganisationDto;
            }
        }

        

        #endregion
    }
}
